/*
 * @Author: i@rysa.cn
 * @Date: 2021-10-13 13:38:48
 * @LastEditTime: 2022-05-20 10:46:41
 * @LastEditors: i@rysa.cn
 * @Description:
 * @FilePath: \go-utils\sms\aliyun_sms\aliyun.go
 */
package aliyun_sms

import (
	"encoding/json"

	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)

type AliyunSms struct {
	cfg    *Config
	client *dysmsapi.Client
}

func NewAliyunSms(cfg *Config) *AliyunSms {
	client, err := dysmsapi.NewClientWithAccessKey(cfg.RegionId, cfg.AccessKey, cfg.AccessKeySecret)
	if err != nil {
		return nil
	}
	return &AliyunSms{cfg, client}
}
func (sms *AliyunSms) SendSms(data SmsContent) map[string]interface{} {
	req := dysmsapi.CreateSendSmsRequest()
	req.Scheme = "https"
	req.PhoneNumbers = data.Phone
	req.SignName = sms.cfg.SignName
	req.TemplateCode = data.TemplateCode
	params, _ := json.Marshal(data.Params)
	req.TemplateParam = string(params)
	resp, _ := sms.client.SendSms(req)
	var m map[string]interface{}
	json.Unmarshal(resp.GetHttpContentBytes(), &m)
	return m
}
